package com.cskaoyan.controller.admin;

import com.cskaoyan.anno.AddLog;
import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.AdminOrderChannelVO;
import com.cskaoyan.bean.vo.AdminOrderDetailVO;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.admin.AdminOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商场管理 --> 订单管理
 *
 * @author : Caspar
 * @since : 2022.06.27 09:01
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService adminOrderService;

    /**
     * 访问订单管理，自动触发的channel,返回快递及其代码，写死的
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/27 21:14
     */
    @RequestMapping("channel")
    public BaseRespVo channel() {
        List<AdminOrderChannelVO> adminOrderChannelVOS = adminOrderService.channel();
        return BaseRespVo.ok(adminOrderChannelVOS);
    }

    /**
     * 分页显示订单管理的返回信息
     * admin/WXUserIndexVo/list?page=1&limit=20&sort=add_time&WXUserIndexVo=desc&start=&end=
     *
     * @param basePageInfo
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/27 15:46
     */
    @RequestMapping("list")
    public BaseRespVo listResponse(BasePageInfo basePageInfo, Integer userId, String orderSn, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end, Short[] orderStatusArray) {

        CommonData<MarketOrder> data = adminOrderService.list(basePageInfo, userId, orderSn, start, end, orderStatusArray);
        return BaseRespVo.ok(data);
    }

    /**
     * 通过订单id查询订单的详细信息
     *
     * @param id
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/27 17:55
     */
    @RequestMapping("detail")
    public BaseRespVo detail(String id) {
        AdminOrderDetailVO adminOrderDetailVO = adminOrderService.detail(id);

        if (StringUtils.isEmpty(adminOrderDetailVO)) {
            return BaseRespVo.error(401, "订单编号不存在！");
        }
        return BaseRespVo.ok(adminOrderDetailVO);
    }

    /**
     * 订单退款接口，map里面封装了 {"orderId":4,"refundMoney":800}
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 10:41
     */
    @AddLog(value = "退款", result = "订单编号", type = 2)
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        // 此处的refundMoney 可能是Integer/Double
        Object money = map.get("refundMoney");
        adminOrderService.refund(orderId, money);
        return BaseRespVo.ok();
    }

    /**
     * 订单发货，通过订单id 和传过来的参数修改各种信息（发货快递、快递编号、订单状态）
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/27 22:27
     */
    @AddLog(value = "发货", result = "订单编号", type = 2)
    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");

        adminOrderService.ship(orderId, shipChannel, shipSn);
        return BaseRespVo.ok();
    }


    @AddLog(value = "删除", result = "订单编号", type = 2)
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        Integer code = adminOrderService.delete(orderId);
        if (code == 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(623, "订单不能删除");
    }

    /**
     * 根据商品评论进行回复,map里面封装了{"commentId":2,"content":"tqr"}
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/27 23:53
     */
    @RequestMapping("reply")
    public BaseRespVo reply(@RequestBody Map map) {

        Integer commentId = (Integer) map.get("commentId");
        String adminContent = (String) map.get("content");
        Integer code = adminOrderService.reply(commentId, adminContent);
        if (code == 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(code, "商品已回复");
    }
}
